package com.leyou.trade.web;

import com.leyou.trade.entity.Carts;
import com.leyou.trade.service.CartService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    private final CartService cartService;

    public CartController(CartService cartService) {
        this.cartService = cartService;
    }


    /**
     * 添加购物车
     * @param carts
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(
            @RequestBody Carts carts) {

        this.cartService.addCart(carts);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Carts>> listCarts(){

        return ResponseEntity.ok(this.cartService.listCarts());
    }

    /**
     * 合并
     */
    @PostMapping("/list")
    public ResponseEntity<Void> mergeCart(
            @RequestBody List<Carts> cartsList){

        this.cartService.mergeCart(cartsList);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
